//
//  LoginViewController.m
//  Home Saigon
//
//  Created by Aka on 11/30/13.
//  Copyright (c) 2013 Le Zoom. All rights reserved.
//

#import "LoginViewController.h"
#import "GUIController.h"

@interface LoginViewController ()

@end

@implementation LoginViewController

-(id)initWithGUI:(GUIController*)_mainGUI{
    self = [super init];
    mainGUI=_mainGUI;
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor =[UIColor whiteColor];
    self.title = @"Login";
    // Add button Checked to save session
    UIImage *imageChecked = [UIImage imageNamed:@"checked.png"];
    UIImage *imageUnchecked =[UIImage imageNamed:@"unchecked.png"];
    UIButton *checkBox = [[UIButton alloc]initWithFrame:CGRectMake(100, 270, imageChecked.size.width, imageChecked.size.height)];
    UILabel *checkBoxText = [[UILabel alloc]initWithFrame:CGRectMake(130, 270, 200, 20)];
    [checkBoxText setText:@"Save your session"];
    //[self.view addSubview:checkBoxText];
    //[self.view addSubview:checkBox];
    [checkBox setImage:imageChecked forState:UIControlStateNormal];
    [checkBox setImage:imageUnchecked forState:UIControlStateSelected];
    [checkBox addTarget:self action:@selector(checkBoxPressed:) forControlEvents:UIControlEventTouchUpInside];
    
    // Add logo Login
    UILabel *Logo =[[UILabel alloc]initWithFrame:CGRectMake(20, 170, 80, 80)];
    UIImage *img = [UIImage imageNamed:@"login.png"];
    [Logo setBackgroundColor:[UIColor colorWithPatternImage:img]];
    //[self.view addSubview:Logo];
    
    //Add Label Header
    UILabel *Header = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
    Header.backgroundColor = [UIColor orangeColor];
    Header.text=@"Home Saigon Real Estate";
    Header.textColor= [UIColor whiteColor];
    Header.font =[UIFont fontWithName:@"Tahoma" size:30];
    Header.textAlignment = NSTextAlignmentCenter;
    //[self.view addSubview:Header];
    
    //Add Text Fields
    AccountField = [[UITextField alloc]initWithFrame:CGRectMake(100, 180, 180, 30)];
    AccountField.borderStyle = UITextBorderStyleBezel;
    AccountField.placeholder =@"Enter your username";
    AccountField.delegate =self;
    //[self.view addSubview:AccountField];
    
    PasswordField = [[UITextField alloc]initWithFrame:CGRectMake(100, 220, 180, 30)];
    PasswordField.borderStyle = UITextBorderStyleBezel;
    PasswordField.placeholder = @"Enter your password";
    PasswordField.delegate=self;
    //[self.view addSubview:PasswordField];
    //Add Button
    UIButton *LogIn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    LogIn.frame = CGRectMake(50, 320, 100, 30);
    LogIn.tag = 1;
    [LogIn setTitle:@"Log In" forState:UIControlStateNormal];
    //[self.view addSubview:LogIn];
    [LogIn addTarget:self action:@selector(btnpressed:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *Register = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    Register.frame = CGRectMake(170, 320, 100, 30);
    Register.tag = 2;
    [Register setTitle:@"Register" forState:UIControlStateNormal];
    [Register addTarget:self action:@selector(btnpressed:) forControlEvents:UIControlEventTouchUpInside];
    //[self.view addSubview:Register];
    _scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    _scrollview.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height*2);
    _scrollview.scrollEnabled = NO ;
    //releas keyboard when click outsied it
    UITapGestureRecognizer *tapGest = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGestPressed:)];
    //Add Objects to Scrollview
    [_scrollview addSubview:checkBoxText];
    [_scrollview addSubview:checkBox];
    [_scrollview addSubview:Logo];
    [_scrollview addSubview:Header];
    [_scrollview addSubview:AccountField];
    [_scrollview addSubview:PasswordField];
    [_scrollview addSubview:Register];
    [_scrollview addSubview:LogIn];
    [_scrollview addGestureRecognizer:tapGest];
    [self.view addSubview:_scrollview];
}
-(void)btnpressed:(id)sender{
    [mainGUI ScreenFinish:self withCode:((UIButton*)sender).tag];
    }
-(void)tapGestPressed:(id)sender{
    [AccountField resignFirstResponder];
    [PasswordField resignFirstResponder];
    [_scrollview setContentOffset:CGPointMake(0, 0) animated:YES];
    
}
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    [_scrollview setContentOffset:CGPointMake(0, 130) animated:YES];
    
}
-(void)checkBoxPressed:(id)sender{
    ((UIButton*)sender).selected = !((UIButton*)sender).selected;
}
-(void)setUsername:(NSString*)_username{
    AccountField.text = _username;
}
-(void)setPassword:(NSString*)_password{
    PasswordField.text = _password;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
